﻿/*************************************************************************
* @File			: DeviceInfo.cs
* @Description	: 设备信息  
*
*
*
*
************************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

namespace Framework
{
    public static class DeviceInfo
    {
        // @brief 不明平台
        public static int PF_UNKOWN = 0;
        // @brief UnityEditor平台
        public static int PF_UNITY_EDITOR = 1;
        // @brief Winodw 平台
        public static int PF_WINDOW = 2;
        // @brief Android 平台
        public static int PF_ANDROID = 3;
        // @brief IOS 平台
        public static int PF_IOS = 4;
        // --------------------------------------------------------------------------------
        // Android平台Unity封装的jni
#if UNITY_ANDROID
        private static AndroidJavaObject _joCurActivity;
        public static AndroidJavaObject JoCurActivity
        {
            get
            {
                if(null == _joCurActivity)
                {
                    AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                    _joCurActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
                }
                return _joCurActivity;
            }
        }
#endif
        // --------------------------------------------------------------------------------
        // @brief 获取平台
        public static int GetPlatfrom()
        {
#if   UNITY_EDITOR
            return PF_UNITY_EDITOR;
#elif UNITY_STANDALONE_WIN
            return PF_WINDOW;
#elif UNITY_ANDROID
            return PF_ANDROID;
#elif UNITY_IOS
            return PF_IOS;
#else
            return PF_UNKOWN;
#endif
        }
        // --------------------------------------------------------------------------------
        // @brief 获取网络状态
        public static int GetNetworkReachabilityType()
        {
            return (int)Application.internetReachability;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取手机电量值为【0-1】之间
        public static float GetBetteryLevel()
        {
            float val = 1.0f;
            try
            {
                val = SystemInfo.batteryLevel;
            }
            catch (Exception ex)
            {
                Logger.LogError("[LogError] GetBetteryLevel ex:" + ex.ToString());
            }
            return val;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取电池状态
        public static BatteryStatus GetBetteryStatus()
        {
            BatteryStatus val = BatteryStatus.Unknown;

            try
            {
                val = SystemInfo.batteryStatus;
            }
            catch (Exception ex)
            {
                Logger.LogError("[LogError] GetBetteryLevel ex:" + ex.ToString());
            }

            return val;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取唯一ID
        public static string GetDeviceUniqueIdentifier()
        {
            return SystemInfo.deviceUniqueIdentifier;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取设备模式
        public static string GetDeviceModel()
        {
            return SystemInfo.deviceModel;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取设备名字
        public static string GetDeviceName()
        {
            return SystemInfo.deviceName;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取设备类型
        public static int GetDeviceType()
        {
            return (int)SystemInfo.deviceType;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取CPU类型
        public static string GetProcessorType()
        {
            return SystemInfo.processorType;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取显卡名字
        public static string GetGraphicsDeviceName()
        {
            return SystemInfo.graphicsDeviceName;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取操作系统
        public static string GetOperatingSystem()
        {
            return SystemInfo.operatingSystem;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取应用名字
        public static string GetAppDisplayName()
        {
            return Application.productName;
        }
        // --------------------------------------------------------------------------------
        // @brief 获取应用版本
        public static string GetAppVersion()
        {
            return Application.version;
        }
        // --------------------------------------------------------------------------------
        // @brief 检测是否为模拟器
        public static bool IsEmulator()
        {
#if UNITY_EDITOR
            return false;
#elif UNITY_ANDROID
            return JoCurActivity.Call<bool>("CheckEmulator");          
#elif UNITY_IOS
            return false;
#else
            return false;
#endif
        }
        // --------------------------------------------------------------------------------
        // @brief 获取是否64位系统
        public static bool IsArm64Device()
        {
#if UNITY_IOS || (UNITY_ANDROID && ENABLE_IL2CPP)
            if (IntPtr.Size == 8)
            {
                return true;
            }
            return false;
#elif UNITY_EDITOR || UNITY_STANDALONE
            return true;
#else
            return false;
#endif
        }
        // --------------------------------------------------------------------------------
        // @brief 是否为编辑器
        public static bool IsEditor()
        {
#if UNITY_EDITOR
            return true;
#else
            return false;
#endif
        }
    }
}
